package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.util.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 患者查看医生详情
 */
public class PatientQueryDoctorInfoActivity extends AppCompatActivity {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private TextView doctorId,doctorName,positionName,departmentsName,specialty,describe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_query_doctor_info);
//        实例化
        dbOpenHelper = new DBOpenHelper(PatientQueryDoctorInfoActivity.this, "db_kindheartedness", null, 1);
//        获取TextView控制权
        doctorId = findViewById(R.id.doctorId);
        doctorName = findViewById(R.id.doctorName);
        positionName = findViewById(R.id.positionName);
        departmentsName = findViewById(R.id.departmentsName);
        specialty = findViewById(R.id.specialty);
        describe = findViewById(R.id.describe);
        Intent intent2 = getIntent();
        Bundle bundle = intent2.getExtras();
        String doctorId = bundle.getString("doctorId");
//        查询医生详情并回显
        findAllDoctorInfo(doctorId);


    }

    /**
     * 查询医生详情并进行回显
     */
    private void findAllDoctorInfo(String doctorId2) {
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();

        Cursor queryDoctorDetails = readableDatabase.query("doctor_details", null, "id = ?", new String[]{doctorId2}, null, null, null);
        while (queryDoctorDetails.moveToNext()) {
            String id = queryDoctorDetails.getString(0);
            String userName = queryDoctorDetails.getString(1);
//            职务ID
            String positionId = queryDoctorDetails.getString(2);
//            科室ID
            String departmentsId = queryDoctorDetails.getString(3);
            String specialty2 = queryDoctorDetails.getString(4);
            String describe2 = queryDoctorDetails.getString(5);
            doctorId.setText(doctorId2);
            doctorName.setText(userName);
            specialty.setText("暂无");
            if (specialty2 != null) {
                specialty.setText("        "+specialty2);
            }
            describe.setText("暂无");
            if (describe2 != null) {
                describe.setText("        "+describe2);
            }
            Cursor queryPosition = readableDatabase.query("position", null, "id = ?", new String[]{positionId}, null, null, null);
            while (queryPosition.moveToNext()) {
                String positionName2 = queryPosition.getString(1);
                positionName.setText(positionName2);
            }
            Cursor queryDepartments = readableDatabase.query("departments", null, "id = ?", new String[]{departmentsId}, null, null, null);
            while (queryDepartments.moveToNext()) {
                String departmentsName2 = queryDepartments.getString(1);
                departmentsName.setText(departmentsName2);
            }
        }
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}